package com.example.expandablelistview.cp;

import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Context;
import android.content.Loader;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class ProductList_CursorLoader extends ListActivity implements LoaderCallbacks<Cursor>{
	
	private SimpleCursorAdapter mAdapter;
	private static final String[] fromColumns = { "name", "price" };
	private static final int[] toViews = { android.R.id.text1, android.R.id.text2 };
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getLoaderManager().initLoader(0, null, this);
	}
	
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new ProductCursorLoader(this);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, data, fromColumns, toViews, 0);
		setListAdapter(mAdapter);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		mAdapter.swapCursor(null);		
	}
	
	final static class ProductCursorLoader extends SimpleCursorLoader {

		private ProductDBHelper mHelper;
		private SQLiteDatabase db;
		
		public ProductCursorLoader(Context context) {
			super(context);
			mHelper = new ProductDBHelper(context);
			db = mHelper.getWritableDatabase();
		}

		@Override
		protected Cursor loadCursor() {
			Cursor cursor = db.rawQuery("SELECT * FROM product", null);
			return cursor;
		}

	}

}

class ProductCursorDBHelper extends SQLiteOpenHelper {
	public ProductCursorDBHelper(Context context) {
		super(context, "ProductCursor.db", null, 1);
	}

	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE product ( _id INTEGER PRIMARY KEY AUTOINCREMENT, " +
			"name TEXT, price INTEGER);");
		db.beginTransaction();
		for(int i=0; i<100; i++){
			db.execSQL("INSERT INTO product VALUES (null, '오징어 땅콩', 900);");
			db.execSQL("INSERT INTO product VALUES (null, '농심 포테이토 칩', 2000);");
			db.execSQL("INSERT INTO product VALUES (null, '로보트 태권 V', 1000);");
			db.execSQL("INSERT INTO product VALUES (null, '꼬마 자동차 붕붕', 1500);");
			db.execSQL("INSERT INTO product VALUES (null, '윈도우즈 API 정복', 32000);");
			db.execSQL("INSERT INTO product VALUES (null, '롯데 인벤스 아파트', 190000000);");
			db.execSQL("INSERT INTO product VALUES (null, '88 라이트', 1900);");
			db.execSQL("INSERT INTO product VALUES (null, '프라이드 1.6 CVVT 골드', 8900000);");
			db.execSQL("INSERT INTO product VALUES (null, '캐리비안 베이 입장권', 25000);");
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS product");
		onCreate(db);
	}
}
